package org.chainsale.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.chainsale.bean.ActiveItem;
import org.chainsale.bean.Practitioner;
import org.chainsale.common.Utils;
import org.chainsale.prefuse.graph.FuseTreePanel;


public class AddNodeActionListener implements ActionListener {

	private int addedNum = 0;
	private String addedName;
	
	private FuseTreePanel treePanel;
	
	public AddNodeActionListener(FuseTreePanel treePanel) {
		this.treePanel = treePanel;
	}
	
	
	public void actionPerformed(ActionEvent e) {
		
		addedNum = 0;
		addedName = null;
		
		Practitioner activePrac = ActiveItem.getInstance().getActivePrac();
		
		if (activePrac == null) {
			StringBuilder msg = new StringBuilder("必须先选中一个直接业务员!");
			JOptionPane.showMessageDialog(null, msg.toString(), "错误", JOptionPane.ERROR_MESSAGE);
			
			return;
		}
		
		getNumber();
		
		if (activePrac.getUnderlings().size() == 3 && addedNum <= 3) {
			StringBuilder msg = new StringBuilder(activePrac.getName()).append("已有3个直接业务员!");
			JOptionPane.showMessageDialog(null, msg.toString(), "错误", JOptionPane.ERROR_MESSAGE);
		} else {
			
			JTextField nameText = ActiveItem.getInstance().getAddedNameText();
			addedName = Utils.isNotEmpty(nameText.getText()) ? nameText.getText().trim() : null;
			
			int existUnderlingNum = activePrac.getUnderlings().size();
			
			if (existUnderlingNum + addedNum <= 3) {
				for (int i=0; i<addedNum; i++) {
					treePanel.addOneNode(activePrac, addedNum, addedName);
				}
			} else {
				int rest = 3 - existUnderlingNum;
				Practitioner p1 = null;
				for (int i=0; i<rest; i++) {
					p1 = treePanel.addOneNode(activePrac, addedNum, addedName);
				}
				
				int gens = (addedNum - rest) / 3;
				int remaining = (addedNum - rest) % 3;
				
				if (p1 == null) {
					p1 = getUnfullNode2(activePrac.getLayer());
				}

				addPractionersByGens(gens, p1);
				
				for (int x=0; x<remaining; x++) {
					treePanel.addOneNode(getUnfullNode2(activePrac.getLayer()), addedNum, addedName);
				}
				
			}
			
			ActiveItem.getInstance().changeNodeProperties();
			ActiveItem.getInstance().changeSystemInfo();
			
			treePanel.repaint();
		}
	}
	
	private void getNumber() {
		String numText = ActiveItem.getInstance().getAddedNumText().getText();
		if (null == numText || numText.trim().isEmpty() || !isNumber(numText.trim())) {
			StringBuilder msg = new StringBuilder(ActiveItem.getInstance().getActivePrac().getName()).append("增加的业务员人数必须为数字!");
			JOptionPane.showMessageDialog(null, msg.toString(), "错误", JOptionPane.ERROR_MESSAGE);
		} else {
			numText = numText.trim();
			
			addedNum = Integer.valueOf(numText);
		}
	}
	
	private void addPractionersByGens(int gens, Practitioner p1) {
		
		int currLayer = ActiveItem.getInstance().getActivePrac().getLayer();
		Practitioner superior = p1;
		while (gens > 0) {
				
			if (superior.getUnderlings().size() == 3) superior = getUnfullNode2(currLayer);
			treePanel.addOneNode(superior, addedNum, addedName);
			
			if (superior.getUnderlings().size() == 3) superior = getUnfullNode2(currLayer);
			treePanel.addOneNode(superior, addedNum, addedName);
			
			if (superior.getUnderlings().size() == 3) superior = getUnfullNode2(currLayer);
			treePanel.addOneNode(superior, addedNum, addedName);
			
			gens--;
		}
		
	}
	
	private Practitioner getUnfullNode2(int currLayer) {
		Practitioner unfull = null;
		
		List<Integer> pIds = Practitioner.LAYER_MAP.get(currLayer);
		Practitioner px = null;
		for (Integer pId : pIds) {
			px = treePanel.getPractitioner(pId);
			if (px.getUnderlings().size() != 3 && ActiveItem.getInstance().getActivePrac().getAllUIds().contains(pId)) {
				unfull = px;
				break;
			}
		}
		
		while (unfull == null) {
			unfull = getUnfullNode2(currLayer + 1);
		}
		
		return unfull;
	}
	
	private boolean isNumber(String numText) {
		boolean isNumber = true;
		for (char c : numText.toCharArray()) {
			if (!Character.isDigit(c)) {
				isNumber = false;
				break;
			}
		}
		
		return isNumber;
	}
	
	
	
}
